package customer

import (
	"context"
	"fmt"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/dao"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/lib/mysql"
	"strconv"
	"time"
)

var (
	AnnouncementSet = wire.NewSet(wire.Struct(new(AnnouncementSrv), "*"))
)

type AnnouncementSrv struct {
	DB *mysql.DB

	AnnouncementRepo *dao.AnnouncementRepo
}

func (a *AnnouncementSrv) Create(ctx context.Context, item map[string]interface{}) interface{} {

	var (
		now = time.Now().Unix()
		def = map[string]interface{}{
			"create_at":  now,
			"updated_at": now,
		}
	)

	for k, v := range def {
		item[k] = v
	}

	return a.AnnouncementRepo.Create(ctx, item)
}

func (a *AnnouncementSrv) Query(ctx context.Context, reqArgs map[string]interface{}, p schema.PaginationParam, repKeys []string) interface{} {

	req, err := a.AnnouncementRepo.Query(ctx, reqArgs, p, repKeys)
	if err != nil {
		return err
	}
	return req
}

func (a *AnnouncementSrv) QueryTypes(ctx context.Context, reqArgs map[string]interface{}, p schema.PaginationParam, repKeys []string) interface{} {

	req, err := a.AnnouncementRepo.Query(ctx, reqArgs, p, repKeys)
	if err != nil {
		return err
	}
	return req
}

func (a *AnnouncementSrv) Enable(ctx context.Context, reqArgs map[string]interface{}) error {

	var (
		now        = time.Now().Unix()
		updateKeys = map[string]interface{}{
			"updated_at": now,
			"status":     1,
		}
		queryKeys = map[string]interface{}{
			"id": reqArgs["id"],
		}
	)

	return a.AnnouncementRepo.Update(ctx, updateKeys, queryKeys)
}

func (a *AnnouncementSrv) Disable(ctx context.Context, reqArgs map[string]interface{}) error {

	var (
		now        = time.Now().Unix()
		updateKeys = map[string]interface{}{
			"updated_at": now,
			"status":     2,
		}
		queryKeys = map[string]interface{}{
			"id": reqArgs["id"],
		}
	)

	return a.AnnouncementRepo.Update(ctx, updateKeys, queryKeys)
}

func (a *AnnouncementSrv) Update(ctx context.Context, reqArgs map[string]interface{}) error {

	var (
		now        = time.Now().Unix()
		updateKeys = map[string]interface{}{
			"updated_at": now,
			"title":      reqArgs["title"],
			"content":    reqArgs["content"],
		}
		queryKeys = map[string]interface{}{
			"id": reqArgs["id"],
		}
	)

	return a.AnnouncementRepo.Update(ctx, updateKeys, queryKeys)
}

func (a *AnnouncementSrv) Delete(ctx context.Context, reqArgs map[string]interface{}) interface{} {

	var (
		id, _ = strconv.ParseUint(fmt.Sprintf("%v", reqArgs["id"]), 10, 64)
	)

	return a.AnnouncementRepo.SoftDelete(ctx, id)
}
